package com.ktgj.hotel.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.hotel.domain.rooms.HotelIncrDataRequest;
import com.ktgj.hotel.domain.rooms.HotelIncrDataResponse;
import com.ktgj.hotel.domain.rooms.HotelIncrIdRequest;
import com.ktgj.hotel.domain.rooms.HotelIncrIdResponse;
import com.ktgj.hotel.utils.NetUtils;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.hotel.domain.HotelSupplierLink;
import com.ktgj.hotel.service.IHotelSupplierLinkService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2021-12-08
 */
@ControllerAdvice
@RestController
@RequestMapping("/link")
public class HotelSupplierLinkController extends BaseController
{
    @Autowired
    private IHotelSupplierLinkService hotelSupplierLinkService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "hotel:link:list")
    @GetMapping("/list")
    public TableDataInfo list(HotelSupplierLink hotelSupplierLink)
    {
        startPage();
        List<HotelSupplierLink> list = hotelSupplierLinkService.selectHotelSupplierLinkList(hotelSupplierLink);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "hotel:link:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HotelSupplierLink hotelSupplierLink) throws IOException
    {
        List<HotelSupplierLink> list = hotelSupplierLinkService.selectHotelSupplierLinkList(hotelSupplierLink);
        ExcelUtil<HotelSupplierLink> util = new ExcelUtil<HotelSupplierLink>(HotelSupplierLink.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize(hasPermi = "hotel:link:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hotelSupplierLinkService.selectHotelSupplierLinkById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "hotel:link:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HotelSupplierLink hotelSupplierLink)
    {
        return toAjax(hotelSupplierLinkService.insertHotelSupplierLink(hotelSupplierLink));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "hotel:link:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HotelSupplierLink hotelSupplierLink)
    {
        return toAjax(hotelSupplierLinkService.updateHotelSupplierLink(hotelSupplierLink));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "hotel:link:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hotelSupplierLinkService.deleteHotelSupplierLinkByIds(ids));
    }

    /**
     * 国内酒店-增量编号
     * @param request
     * @return
     */
    @PostMapping("/hotelIncrId")
    public AjaxResult hotelIncrId(@RequestBody HotelIncrIdRequest request) {
        HotelIncrIdResponse response = hotelSupplierLinkService.hotelIncrId(request);
        if (response.getCode().equals("0")) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 国内酒店-酒店增量
     * @param request
     * @return
     */
    @PostMapping("/hotelIncrData")
    public AjaxResult hotelIncrData(@RequestBody HotelIncrDataRequest request) {
        int count = 1000;
        Long lastId = 1l;
        List<Long> longList = new ArrayList<>();
        HotelIncrDataResponse hotelIncrDataResponse;
        List<Long> id = new ArrayList<>();
        do {
//            HotelIncrDataRequest request = new HotelIncrDataRequest();
            request.setLastId(lastId);
            request.setCount(count);
            hotelIncrDataResponse = hotelSupplierLinkService.hotelIncrData(request);
            if (hotelIncrDataResponse.hotels.size() > 0){
                for (Long hotel : hotelIncrDataResponse.hotels) {
                    id.add(hotel);
                }
            }
            if (hotelIncrDataResponse.getResult().getHotels() != null && hotelIncrDataResponse.getResult().getHotels().size() > 0){
                for (HotelIncrDataResponse.HotelResult.Hotel hotel : hotelIncrDataResponse.getResult().getHotels()) {
                    longList.add(hotel.getLastId());
                }
                longList.sort((x, y) -> Double.compare(y, x));
                lastId = longList.get(0);
            }
        } while (hotelIncrDataResponse.getResult().getHotels() != null && hotelIncrDataResponse.getResult().getHotels().size() > 0);
        return AjaxResult.success(id);
//        HotelIncrDataResponse response = hotelSupplierLinkService.hotelIncrData(request);
//        if (response.getCode().equals("0")) {
//            return AjaxResult.success(response);
//        } else {
//            return AjaxResult.error("增量失败");
//        }
    }
}
